package com.xxx.user.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.user.domain.Dict;
import com.xxx.user.domain.DictItem;
import com.xxx.user.query.DictQuery;
import com.xxx.user.service.IDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dict")
@Api(tags = "字典接口类")
public class DictController {
    @Autowired
    private IDictService dictService;
    @GetMapping
    @ApiOperation(value = "查询所有")
    public R<List<Dict>> findAll() {
        List<Dict> list = dictService.findAll();
        return R.ok(list);
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public R<Dict> findOne(@PathVariable Long id) {
        Dict dict = dictService.findOne(id);
        return R.ok(dict);
    }
    @PostMapping
    @ApiOperation(value = "分页条件查询")
    public R<PageInfo<Dict>> queryPage(@RequestBody DictQuery dictQuery) {
        List<Dict> list = dictService.queryPage(dictQuery);
        return R.ok(new PageInfo<Dict>(list));
    }

    @PutMapping
    @ApiOperation(value = "添加或者删除")
    public R<Void> queryPage(@RequestBody Dict dict) {
        try {
            if(dict.getId()==null){
                dictService.addDict(dict);
            }else {
                dictService.updateDict(dict);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("用户已存在");
        }
        return R.ok();
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public R<Void> deleteDict(@PathVariable Long id) {
        try {
            dictService.deleteDict(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }
    @PatchMapping
    @ApiOperation(value = "根据多个id删除")
    public R<Void> batchDict(@RequestBody Long[] ids) {
        try {
            dictService.batchDict(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @GetMapping("/getdictItem/{id}")
    @ApiOperation(value = "抽屉查询")
    public R<List<DictItem>> getdict(@PathVariable Long id) {
        List<DictItem> dictItems = dictService.getdictItem(id);
        return R.ok(dictItems);
    }
}
